package com.smforj.ssm.frame.core.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;

import com.smforj.ssm.frame.core.bean.SysCache;
import com.smforj.ssm.frame.core.bean.Templete;
import com.smforj.ssm.frame.core.enums.SystemInfo;
import com.smforj.ssm.util.PropKit;

/****
 * 系统配置Filter 
 * 系统加载时要加载的信息在该对象中进行配置
 *
 * @author Haijun Gao 
 * @date 2016-9-9 下午2:11:31
 * @desp http://www.cnblogs.com/saga5998/
 * @email 573107773@qq.com
 */
public class GlobalConfigFilter extends HttpServlet implements Filter {

    /**
     *
     */
    private static final long serialVersionUID = 6594619436075541922L;
    private Logger logger = Logger.getLogger(this.getClass());

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    	if(logger.isDebugEnabled())
    		logger.debug("SysConfigFilter init=============================");
        loadsys();
    }

    /***
     * 加载系统配置信息
     *
     * @date 2016-9-9 下午2:19:13
     */
    private void loadsys() {
    	//已经提前到listener 中加载 
/*        PropKit.use("smforj.properties");
        if(logger.isDebugEnabled())
        	logger.debug("load sys.properties=============================end");*/

        SystemInfo sys = new SystemInfo();
        sys.setDefaultAdminTemplete(PropKit.get("default_front_templete"));
        sys.setDefaultAdminTemplete(PropKit.get("default_admin_templete"));
        SysCache.setSys(sys);
        if(logger.isDebugEnabled())
        	logger.debug("load templete=============================end"); 
        
/*        String hostName = PropKit.get("socketHostName");
        int hostPort = Integer.parseInt(PropKit.get("socketHostPort"));
        SocketAddress socketAddress = new InetSocketAddress(hostName, hostPort);
        SocketServiceImpl socketService = new SocketServiceImpl();
        SysCache.setSocketService(socketService);
        SysCache.setSocketAddress(socketAddress);

        if(logger.isDebugEnabled())
        	logger.debug("配置socket地址=============================end");*/

        //默认为后台模板
        Templete templete = new Templete();
        templete.setDefault_templete(PropKit.get("default_admin_templete"));
        SysCache.setTemplete(templete);
        if(logger.isDebugEnabled())
        	logger.debug("set 后台模板======================================end");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
	  if(logger.isDebugEnabled())
		  logger.debug("GlobalConfigFilter doFilter============================="+request.getRemoteAddr());
        chain.doFilter(request, response);

    }

}
